﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using libInventory.BUS;
using Telerik.WinControls.UI;
using libDAL;

namespace libInventory.FORMS
{
    public partial class frmFindSalesTermHeader : Telerik.WinControls.UI.RadForm
    {
        private clsInterfaceSalesTerm _BUS;
        private string strHeaderID = string.Empty;
        private string strPortofOrigin = string.Empty;
        private string strPortofDestination = string.Empty;
        private string strSalesTerm = string.Empty;

        public frmFindSalesTermHeader()
        {
            InitializeComponent();
            _BUS = new clsInterfaceSalesTerm();
        }

        public string getset_SalesTerm
        {
            get
            {
                return strSalesTerm;
            }
            set
            {
                strSalesTerm = value; 
            }
        }

        public string getset_PortOfOriginID
        {
            get
            {
                return strPortofOrigin; 
            }
            set
            {
                strPortofOrigin = value; 
            }
        }

        public string getset_PortOfDestination
        {
            get
            {
                return strPortofDestination; 
            }
            set
            {
                strPortofDestination = value; 
            }
        }

        public string get_HeaderID
        {
            get
            {
                return this.strHeaderID;
            }
        }

        private void set_SearchRecords()
        {
            try
            {
                DataTable dt = new DataTable();

                dt = _BUS.get_SalesTermsHeader(ddlIncoterms.Text.Trim(), ddlType.Text.Trim(), ddlCategory.Text.Trim(),
                                                                mcmbOrigin.Text.Trim(),
                                                                mcmbDestination.Text.Trim(), dteFrom.Value, dteTo.Value);

                dgvHeader.DataSource = dt.Copy();

                if (dgvHeader.Columns.Count > 0)
                {
                    dgvHeader.Columns[0].IsVisible = false;
                    dgvHeader.BestFitColumns();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.InnerException.Message + " set_SearchRecords"); 
            }
        }

        private void ddlIncoterms_SelectedIndexChanged(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
        {
            if (this.IsLoaded == true)
            {
                set_SearchRecords();
            }
             
         }

        private void dteFrom_ValueChanged(object sender, EventArgs e)
        {
            if (this.IsLoaded == true)
            {
                set_SearchRecords();
            }
        }

        private void set_LoadPortOrigin()
        {
            try
            {
                DataTable dt = new DataTable();
                dt = _BUS.get_Ports();
                mcmbOrigin.EditorControl.LoadElementTree();
                mcmbOrigin.DataSource = dt;

                for (int intIndex = 0; intIndex < dt.Columns.Count; intIndex++)
                {
                    mcmbOrigin.MultiColumnComboBoxElement.Columns[intIndex].BestFit();
                }

                mcmbOrigin.MultiColumnComboBoxElement.DropDownWidth = 300;
                mcmbOrigin.MultiColumnComboBoxElement.DropDownHeight = 150;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " set_LoadPortOrigin"); 
            }
        }

        private void set_LoadPortDestination()
        {
            try
            {
                DataTable dt = new DataTable();
                dt = _BUS.get_Ports();
                mcmbDestination.EditorControl.LoadElementTree();
                mcmbDestination.DataSource = dt;

                for (int intIndex = 0; intIndex < dt.Columns.Count; intIndex++)
                {
                    mcmbDestination.MultiColumnComboBoxElement.Columns[intIndex].BestFit();
                }

                mcmbDestination.MultiColumnComboBoxElement.DropDownWidth = 300;
                mcmbDestination.MultiColumnComboBoxElement.DropDownHeight = 150;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " set_LoadPortDestination"); 
            }
        }

        private void frmFindSalesTermHeader_Load(object sender, EventArgs e)
        {
            try
            {
                set_LoadPortDestination();
                set_LoadPortOrigin();

                dteFrom.Value = clsGenFunction.get_DateDBServerFormat(_BUS.get_DAL);
                dteTo.Value = clsGenFunction.get_DateDBServerFormat(_BUS.get_DAL);

                if (getset_PortOfDestination != string.Empty && getset_PortOfOriginID != string.Empty && getset_SalesTerm != string.Empty)
                {
                    ddlIncoterms.Text = getset_SalesTerm;
                    mcmbDestination.Text = getset_PortOfDestination;
                    mcmbOrigin.Text = getset_PortOfOriginID;  
                    set_SearchRecords();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.InnerException.Message + " frmFindSalesTermHeader_Load"); 
            }
            
        }

        private void radButton1_Click(object sender, EventArgs e)
        {

        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (dgvHeader.SelectedRows.Count > 0)
            {
                this.strHeaderID = dgvHeader.CurrentRow.Cells["HeaderID"].Value.ToString();
                DialogResult = DialogResult.OK;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void dgvHeader_DoubleClick(object sender, EventArgs e)
        {
            if (dgvHeader.SelectedRows.Count > 0)
            {
                this.strHeaderID = dgvHeader.CurrentRow.Cells["HeaderID"].Value.ToString();
                DialogResult = DialogResult.OK;
            }
        }
    }
}
